﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace CoPro.Provisioning
{
    /// <summary>
    /// This class retrieves the content from the configured ContentProvider.
    /// </summary>
    internal class ContentRetriever
    {
        internal IContentProvider ContentProvider { get; set; }
        internal String ContentKey { get; set; }

        internal ContentRetriever(SiteContent _currentControl)
        {
            ContentProvider = ContentProviderFactory.GetContentProvider(_currentControl.ProviderName);

            //check and set ContentKey, if key is invalid, throw exception
            if ((ContentKey = _currentControl.Key) == null)
                throw new InvalidOperationException("Key for SiteContent control is not set.");

        }

        internal void Write(TextWriter writer)
        {
            String content = ContentProvider.GetContent(ContentKey, CoProManager.Current.LocalProvider);

            writer.Write(content);

        }
    }
}
